<template>
  <div class="container">
    <h1>新建楼座</h1>
    <hr style="border: 1px solid rgb(240, 240, 240)" />
    <span>基础信息</span>
    <div class="body">
      <div class="input">
        <div class="input-left">
          <span>楼座名称</span>
        </div>
        <div class="input-right">
          <el-input v-model="floor_name" placeholder="请输入" />
        </div>
      </div>
      <div class="input">
        <div class="input-left">
          <span>建成日期</span>
        </div>
        <div class="input-right">
          <el-date-picker
            v-model="create_time"
            style="width: 300px"
            type="date"
            placeholder="选择时间"
          />
        </div>
      </div>
      <div class="input">
        <div class="input-left">
          <span>房屋用途</span>
        </div>
        <div class="input-right">
          <el-select v-model="floor_use" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </div>
      </div>
      <div class="input">
        <div class="input-left">
          <span>楼宇面积</span>
        </div>
        <div class="input-right">
          <el-input v-model="floor_area" type="number" placeholder="请输入" />
        </div>
      </div>
      <div class="input">
        <div class="input-left">
          <span>单元数量</span>
        </div>
        <div class="input-right">
          <el-input v-model="unit_num" type="number" placeholder="请输入" />
        </div>
      </div>
      <div class="input">
        <div class="input-left">
          <span>单元楼层数量</span>
        </div>
        <div class="input-right">
          <el-input
            v-model="unit_floor_num"
            type="number"
            placeholder="请输入"
          />
        </div>
      </div>
      <div class="input">
        <div class="input-left">
          <span>电梯</span>
        </div>
        <div class="input-right">
          <el-radio-group v-model="elevator">
            <el-radio value="有">有</el-radio>
            <el-radio value="无">无</el-radio>
          </el-radio-group>
        </div>
      </div>
      <div class="input">
        <div class="input-left">
          <span>储物室</span>
        </div>
        <div class="input-right">
          <el-radio-group v-model="storage">
            <el-radio value="有">有</el-radio>
            <el-radio value="无">无</el-radio>
          </el-radio-group>
        </div>
      </div>
      <div class="input">
        <div class="input-left">
          <span>备注</span>
        </div>
        <div class="input-right">
          <el-input v-model="remark" :rows="5" type="textarea" />
        </div>
      </div>
      <div style="margin: 2rem 0 2rem 12rem">
        <el-button type="primary" @click="addOrUpdate">提交</el-button>
        <el-button type="default" @click="to('file/floor')">返回</el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import to from "@/utils/to.js";
import axios from "axios";
import dayjs from "dayjs";
import { ElMessage } from "element-plus";
import { useRoute } from "vue-router";

const route = useRoute();
const id = route.query.id;

const floor_name = ref("");
const create_time = ref("");
const floor_use = ref("");
const floor_area = ref(0);
const unit_num = ref(0);
const unit_floor_num = ref(0);
const elevator = ref("");
const storage = ref("");
const remark = ref("");

const options = [
  {
    value: "普通住宅",
    label: "普通住宅",
  },
  {
    value: "商业用地",
    label: "商业用地",
  },
  {
    value: "商住两用",
    label: "商住两用",
  },
  {
    value: "其他",
    label: "其他",
  },
];

const addOrUpdate = () => {
  let config = {};

  if (id === undefined || id === "") {
    config = {
      url: "http://localhost:8080/floor/add",
      method: "POST",
      headers: {
        Authorization: localStorage.getItem("token"),
      },
      data: {
        name: floor_name.value,
        createTime: dayjs(create_time.value).format("YYYY-MM-DD"),
        houseUser: floor_use.value,
        floorAreas: floor_area.value,
        unitNum: unit_num.value,
        floorNum: unit_floor_num.value,
        elevator: elevator.value,
        storage: storage.value,
        detail: remark.value,
        communityId: "1",
      },
    };
  } else {
    config = {
      url: "http://localhost:8080/floor/update",
      method: "POST",
      headers: {
        Authorization: localStorage.getItem("token"),
      },
      data: {
        id: id,
        name: floor_name.value,
        createTime: dayjs(create_time.value).format("YYYY-MM-DD"),
        houseUser: floor_use.value,
        floorAreas: floor_area.value,
        unitNum: unit_num.value,
        floorNum: unit_floor_num.value,
        elevator: elevator.value,
        storage: storage.value,
        detail: remark.value,
        communityId: "1",
      },
    };
  }

  const response = axios(config);
  response
    .then((data) => {
      if (data.data.code === 1) {
        ElMessage({
          message: data.data.msg,
          type: "success",
        });
        setTimeout(() => {
          to('file/floor');
        }, 2000);
      }
    })
    .catch((error) => {
      console.log(error);
    });
};

const get = () => {
  const config = {
    url: `http://localhost:8080/floor/get/${id}`,
    method: "GET",
    headers: {
      Authorization: localStorage.getItem("token"),
    },
  };

  const response = axios(config);
  response
    .then((data) => {
      floor_name.value = data.data.data.name;
      create_time.value = data.data.data.createTime;
      floor_use.value = data.data.data.houseUser;
      floor_area.value = data.data.data.floorAreas;
      unit_num.value = data.data.data.unitNum;
      unit_floor_num.value = data.data.data.floorNum;
      elevator.value = data.data.data.elevator;
      storage.value = data.data.data.storage;
      remark.value = data.data.data.detail;
    })
    .catch((error) => {
      console.log(error);
    });
};

onMounted(() => {
  get();
});
</script>

<style scoped>
.container {
  background-color: white;
  height: auto;
  margin-top: 2rem;
  margin-bottom: 2rem;
}

h1 {
  font-size: 2rem;
  font-weight: 600;
  padding: 2rem 0 10px 4rem;
}

span {
  display: inline-block;
  font-size: 15px;
  margin: 1rem 2rem 1rem 0;
  line-height: 24px;
  width: 100px;
  text-align: right;
}

.body {
  display: grid;
  justify-content: center;
  align-items: center;
}

.input {
  display: flex;
  justify-content: center;
  align-items: center;
  width: auto;
}

.input-right {
  width: 300px;
}
</style>
